package com.banciyuan.bcywebview.base.view.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.biz.write.photoselecotor.util.CommonUtils;
import com.banciyuan.bcywebview.utils.common.UIUtils;


public class TimelineBanDialog extends Dialog {
    public TimelineBanDialog(Context context) {
        super(context);
    }

    public TimelineBanDialog(Context context, int theme) {
        super(context, theme);
    }

    public TimelineBanDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public static class Builder {
        private Context mContext;
        private View viewBanContent, viewBanAndReportContent;
        private View up, down, container;
        private TextView viewBanTimelineType;
        private View.OnClickListener banContentListener, banAndReportContentListener, banTimelineTypeListener;
        private String timelineTypeText;
        private String type;
        private int top;

        public Builder(Context context) {
            mContext = context;
        }

        public Builder setTop(int top) {
            this.top = top;
            return this;
        }

        public Builder setType(String type) {
            this.type = type;
            return this;
        }

        public Builder setBanContentListener(View.OnClickListener banContentListener) {
            this.banContentListener = banContentListener;
            return this;
        }

        public Builder setBanAndReportContentListener(View.OnClickListener banAndReportContentListener) {
            this.banAndReportContentListener = banAndReportContentListener;
            return this;
        }

        public Builder setBanTimelineTypeListener(View.OnClickListener banTimelineTypeListener) {
            this.banTimelineTypeListener = banTimelineTypeListener;
            return this;
        }

        public Builder setBanTimelineTypeText(String timelineTypeText) {
            this.timelineTypeText = timelineTypeText;
            return this;
        }

        public TimelineBanDialog create() {
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View layout = inflater.inflate(R.layout.timelineban_selector_dialog_layout, null);
            final TimelineBanDialog mydialog = new TimelineBanDialog(mContext, R.style.DialogTimeline);
            viewBanContent = layout.findViewById(R.id.dialog_bancontent);
            up = layout.findViewById(R.id.up);
            down = layout.findViewById(R.id.down);
            container = layout.findViewById(R.id.container);
            viewBanAndReportContent = layout.findViewById(R.id.dialog_ban_report_content);
            viewBanTimelineType = (TextView) layout.findViewById(R.id.dialog_ban_timelinetype);
            viewBanTimelineType.setText(timelineTypeText);
            dealTop();
            if ("spot".equals(type)) {
                ((View) (viewBanAndReportContent.getParent())).setVisibility(View.GONE);
                ((View) (viewBanTimelineType.getParent())).setVisibility(View.GONE);
            } else {
                setBanAndReportListener(mydialog);
                setBanTimelinetypeListener(mydialog);
            }
            setBanContentListener(mydialog);
            layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mydialog.dismiss();
                }
            });
            mydialog.setContentView(layout);
            return mydialog;
        }

        private void setBanContentListener(final TimelineBanDialog mydialog) {
            ((View) (viewBanContent.getParent())).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null != banContentListener) {
                        banContentListener.onClick(v);
                    }
                    mydialog.dismiss();
                }
            });
        }

        private void setBanTimelinetypeListener(final TimelineBanDialog mydialog) {
            ((View) (viewBanTimelineType.getParent())).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null != banTimelineTypeListener) {
                        banTimelineTypeListener.onClick(v);
                    }
                    mydialog.dismiss();
                }
            });
        }

        private void setBanAndReportListener(final TimelineBanDialog mydialog) {
            ((View) (viewBanAndReportContent.getParent())).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null != banAndReportContentListener) {
                        banAndReportContentListener.onClick(v);
                    }
                    mydialog.dismiss();
                }
            });
        }

        private void dealTop() {
            int height = CommonUtils.getHeightPixels((Activity) mContext) - CommonUtils.getStatusPixels((Activity) mContext);
            if (height / 2 < top) {
                up.setVisibility(View.GONE);
                down.setVisibility(View.VISIBLE);
                int offset = 0;
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    offset = 130;
                } else {
                    offset = 150;
                }
                RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) container.getLayoutParams();
                layoutParams.setMargins(0, top - UIUtils.dip2px(offset, mContext), 0, 0);


            } else {
                up.setVisibility(View.VISIBLE);
                down.setVisibility(View.GONE);
                int offset = 0;
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    offset = 68;
                } else {
                    offset = 48;
                }
                RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) up.getLayoutParams();
                layoutParams.setMargins(0, top + UIUtils.dip2px(offset, mContext), UIUtils.dip2px(14, mContext), 0);
            }
        }
    }


}
